package com.lmaos.basetools.timer;

import java.lang.reflect.Method;

import com.lmaos.allcontrol.beancontrol.entity_.LMClassParam;
import com.lmaos.allcontrol.beancontrol.utils_.ObjUtils;
import com.lmaos.basetools.base.SParamMap;
import com.lmaos.basetools.sText.TextUtils;

public class BaseTimerThread extends Thread{
	private Method method = null;
	private SParamMap paramMap = null;
	private long ms = 0;
	private Object obj;
	private BaseTimerConfigGroup beanTimerConfigGroup;
	public BaseTimerThread(SParamMap paramMap,Method method,Object obj,BaseTimerConfigGroup beanTimerConfigGroup) {
		super();
		this.method = method;
		this.paramMap = paramMap;
		this.obj = obj;
		this.beanTimerConfigGroup = beanTimerConfigGroup;
		BaseTimer baseTimer = method.getAnnotation(BaseTimer.class);
		if(baseTimer!=null){
			System.out.println("timer name->"+baseTimer.name()+",des->"+baseTimer.des());
			ms = baseTimer.ms()
					+baseTimer.ss()*1000
					+baseTimer.mi()*60*1000
					+baseTimer.hh()*60*60*1000;	// 定时毫秒
			if(!TextUtils.isBlank(baseTimer.name())){
				setName(baseTimer.name());
			}
		}
	}

	@Override
	public void run() {
		try {
			LMClassParam classParam = new LMClassParam();
			classParam.put(beanTimerConfigGroup);
			classParam.put(paramMap);
			classParam.put(this);
			while(ms>0){
				try {
					ObjUtils.executeMethod(obj, method, classParam);
				} catch (Exception e) {
					e.printStackTrace();
				}
				sleep(ms);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public long getMs() {
		return ms;
	}
}
